﻿using System;
using System.Linq;
using Solution.BusinessObject;

namespace Solution.Controls
{
    public partial class AlbumSlider : System.Web.UI.UserControl
    {
        public int AlbumId;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (AlbumId == 0)
            {
                if (Request["abl"] != null)
                {
                    this.AlbumId = Convert.ToInt32(Request["abl"]);
                }
            }

            if (AlbumId <= 0)
            {
                return;
            }

            var album = CommonBO.MasterEDM.Contents.FirstOrDefault(t => t.Id == AlbumId);
            if (album == null)
            {
                return;
            }

            album.Views = (short?) (album.Views == null ? 1 : album.Views + 1);

            GetImagesInAlbum();

            CommonBO.MasterEDM.SaveChanges();
        }

        private void GetImagesInAlbum()
        {
            if (this.AlbumId <= 0)
            {
                return;
            }

            var images = CommonBO
                .MasterEDM
                .DetailContents
                .Where(t => t.Content.Id == AlbumId)
                .ToArray();
            rptAlbumSlider.DataSource = images;
            rptAlbumSlider.DataBind();
        }
    }
}
